package file;

import ipconfig.Ipconfig;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;

import voSetting.FileVO;

public class FileDownload extends Thread {
	
	Socket socket = null;
	Ipconfig ip = new Ipconfig();
	
	int portNumber;
	FileClientThread fileClientThread = null;
	Vector<FileInfoVO> vector_fileInfo = null;
	Vector<FileTransferPanel> vectorFileTransferPanels = null;
	
	OutputStream os = null;
	ObjectOutputStream oos = null;
	
	InputStream is = null;
	ObjectInputStream ois = null;	
	
	FileOutputStream fos = null;
	BufferedOutputStream bos = null;
	
	boolean flag = false;
	int fileSize;
	
	public FileDownload(FileClientThread fileClientThread , int portNumber, Vector<FileInfoVO> vector_fileInfo, Vector<FileTransferPanel> vectorFileTransferPanels) {
		
		this.fileClientThread = fileClientThread;
		this.portNumber = portNumber;
		this.vector_fileInfo = vector_fileInfo;
		this.vectorFileTransferPanels = vectorFileTransferPanels;
		
		try {
			socket = new Socket(ip.getFIXED_IP(), portNumber);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.start();
	}
	
	@Override
	public void run() {
		
		try {
			ArrayList<FileVO> al = null;
			int count = 0;
			ois = new ObjectInputStream(socket.getInputStream());
			
			while(true){
				System.out.println("파일 수신 작업을 시작합니다.");
				if(flag){
					al = (ArrayList<FileVO>)ois.readObject();
					
					for(int i = 0; i < al.size(); i++){
						FileVO fvo = al.get(i);
						System.out.println("서버--------------------------------------------------------------------------");
						System.out.println("파일명 ("+fvo.getPanel_fileName()+")을 전송받았습니다.");
						System.out.println("client가 넘긴 oject(panel_fileName)값: "+fvo.getPanel_fileName());
						System.out.println("client가 넘긴 oject(panel_filePath)값: "+fvo.getPanel_filePath());
						System.out.println("client가 넘긴 oject(panel_fileSize)값: "+fvo.getPanel_fileSize());
						System.out.println("client가 넘긴 oject(panel_size)값: "+fvo.getPanel_size());
						System.out.println("count : " + count);
					}
					flag = false;
				}
				FileVO fvo = al.get(count);
				System.out.println("파일명 ("+fvo.getPanel_fileName()+")을 전송받았습니다.");
				System.out.println("client가 넘긴 oject(panel_fileName)값: "+fvo.getPanel_fileName());
				System.out.println("client가 넘긴 oject(panel_filePath)값: "+fvo.getPanel_filePath());
				System.out.println("client가 넘긴 oject(panel_fileSize)값: "+fvo.getPanel_fileSize());
				System.out.println("client가 넘긴 oject(panel_size)값: "+fvo.getPanel_size());
				
				String path = "C:/Client/";
				File f = new File(path);
				if(!f.exists()) f.mkdirs();
				
				fos = new FileOutputStream(path+fvo.getPanel_fileName());
				bos = new BufferedOutputStream(fos);
				System.out.println(fvo.getPanel_fileName()+ "파일을 생성하였습니다.");
				
				int len;
				byte[] data = new byte[1024];
				while((len = ois.read(data)) != -1 ){
					fileSize += len;
					System.out.println("len 크기 : " + len);
					System.out.println(path+fvo.getPanel_fileName()+"수신중");
					bos.write(data, 0, len);
					System.out.println("fos사이즈 : " +  fvo.getPanel_fileSize());
					if(fileSize == fvo.getPanel_fileSize()){
						bos.flush();
						System.out.println("if 문장 진입");
						break;
					}
				}
				System.out.println("받은 파일의 사이즈: "+fileSize+"Bytes");
				System.out.println("while문장 밖으로 나옴");
				len = 0;
				
				bos.close();
				fos.close();	
				count ++;
				if(fvo.getPanel_size() == count){
					System.out.println("종료됨");
					ois.close();
					break;
				}
				System.out.println("파일 수신을 완료하였습니다.");
				System.out.println("받은 파일의 사이즈: "+fileSize+"Bytes");
				fileSize = 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("파일 수신에 실패 했습니다.");
		}
	}
}
